<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.2.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.png">
  <link rel="mask-icon" href="/images/favicon.png" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" integrity="sha256-Z1K5uhUaJXA7Ll0XrZ/0JhX4lAtZFpT6jkKrEDT0drU=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"it-liupp.gitee.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.14.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":true,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="摘要：记录Kubernetes的常用命令，搜刮自互联网….">
<meta property="og:type" content="article">
<meta property="og:title" content="Kubernetes常用命令">
<meta property="og:url" content="https://it-liupp.gitee.io/2024/01/19/kubernetes-commands/index.html">
<meta property="og:site_name" content="Hello Mr Liu">
<meta property="og:description" content="摘要：记录Kubernetes的常用命令，搜刮自互联网….">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2024-01-19T02:10:10.000Z">
<meta property="article:modified_time" content="2024-01-23T03:36:31.121Z">
<meta property="article:author" content="Mr Liu">
<meta property="article:tag" content="Kubernetes">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://it-liupp.gitee.io/2024/01/19/kubernetes-commands/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://it-liupp.gitee.io/2024/01/19/kubernetes-commands/","path":"2024/01/19/kubernetes-commands/","title":"Kubernetes常用命令"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Kubernetes常用命令 | Hello Mr Liu</title>
  






  <script async defer data-website-id="" src=""></script>

  <script defer data-domain="" src=""></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Hello Mr Liu</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">临江对月</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
  </ul>
</nav>




</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%9B%86%E7%BE%A4%E4%BF%A1%E6%81%AF"><span class="nav-number">1.</span> <span class="nav-text">集群信息</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Pod-%E8%AF%8A%E6%96%AD-amp-amp-%E5%81%A5%E5%BA%B7%E6%A3%80%E6%9F%A5"><span class="nav-number">2.</span> <span class="nav-text">Pod 诊断&amp;&amp;健康检查</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Service%E8%AF%8A%E6%96%AD"><span class="nav-number">3.</span> <span class="nav-text">Service诊断</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Deployment%E8%AF%8A%E6%96%AD"><span class="nav-number">4.</span> <span class="nav-text">Deployment诊断</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#StatefulSet%E8%AF%8A%E6%96%AD"><span class="nav-number">5.</span> <span class="nav-text">StatefulSet诊断</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ConfigMap-%E5%92%8CSecret%E8%AF%8A%E6%96%AD"><span class="nav-number">6.</span> <span class="nav-text">ConfigMap 和Secret诊断</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%91%BD%E5%90%8D%E7%A9%BA%E9%97%B4%E8%AF%8A%E6%96%AD"><span class="nav-number">7.</span> <span class="nav-text">命名空间诊断</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%B5%84%E6%BA%90%E4%BD%BF%E7%94%A8%E6%83%85%E5%86%B5"><span class="nav-number">8.</span> <span class="nav-text">资源使用情况</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BD%91%E7%BB%9C%E8%AF%8A%E6%96%AD"><span class="nav-number">9.</span> <span class="nav-text">网络诊断</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8C%81%E4%B9%85%E5%8D%B7-PV-%E5%92%8C%E6%8C%81%E4%B9%85%E5%8D%B7%E5%A3%B0%E6%98%8E-PVC-%E8%AF%8A%E6%96%AD"><span class="nav-number">10.</span> <span class="nav-text">持久卷 (PV) 和持久卷声明 (PVC) 诊断</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%8A%82%E7%82%B9%E8%AF%8A%E6%96%AD"><span class="nav-number">11.</span> <span class="nav-text">节点诊断</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%B5%84%E6%BA%90%E9%85%8D%E9%A2%9D%E5%92%8C%E9%99%90%E5%88%B6"><span class="nav-number">12.</span> <span class="nav-text">资源配额和限制</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E8%B5%84%E6%BA%90%E5%AE%9A%E4%B9%89-CRD-%E8%AF%8A%E6%96%AD"><span class="nav-number">13.</span> <span class="nav-text">自定义资源定义 (CRD) 诊断</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%B5%84%E6%BA%90%E4%BC%B8%E7%BC%A9%E5%92%8C%E8%87%AA%E5%8A%A8%E4%BC%B8%E7%BC%A9"><span class="nav-number">14.</span> <span class="nav-text">资源伸缩和自动伸缩</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%9C%E4%B8%9A%E5%92%8C-CronJob-%E8%AF%8A%E6%96%AD"><span class="nav-number">15.</span> <span class="nav-text">作业和 CronJob 诊断</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%B9%E9%87%8F%E8%AF%8A%E6%96%AD"><span class="nav-number">16.</span> <span class="nav-text">容量诊断</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Ingress%E5%92%8C%E6%9C%8D%E5%8A%A1%E7%BD%91%E6%A0%BC%E8%AF%8A%E6%96%AD"><span class="nav-number">17.</span> <span class="nav-text">Ingress和服务网格诊断</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Pod-%E7%9B%B8%E5%85%B3"><span class="nav-number">18.</span> <span class="nav-text">Pod 相关</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%85%8D%E7%BD%AE%E5%92%8C%E8%B5%84%E6%BA%90%E9%AA%8C%E8%AF%81"><span class="nav-number">19.</span> <span class="nav-text">配置和资源验证</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#RBAC-%E5%92%8C%E5%AE%89%E5%85%A8%E6%80%A7"><span class="nav-number">20.</span> <span class="nav-text">RBAC 和安全性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9C%8D%E5%8A%A1%E5%B8%90%E6%88%B7%E8%AF%8A%E6%96%AD"><span class="nav-number">21.</span> <span class="nav-text">服务帐户诊断</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B8%85%E7%A9%BA%E8%8A%82%E7%82%B9%E5%92%8C%E8%A7%A3%E9%99%A4%E5%B0%81%E9%94%81"><span class="nav-number">22.</span> <span class="nav-text">清空节点和解除封锁</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%8B%E4%BB%B6"><span class="nav-number">23.</span> <span class="nav-text">事件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%8A%82%E7%82%B9%E6%95%85%E9%9A%9C%E6%8E%92%E9%99%A4"><span class="nav-number">24.</span> <span class="nav-text">节点故障排除</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%B4%E6%97%B6%E5%AE%B9%E5%99%A8%EF%BC%88Kubernetes-1-18-%EF%BC%89"><span class="nav-number">25.</span> <span class="nav-text">临时容器（Kubernetes 1.18+）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%B5%84%E6%BA%90%E6%8C%87%E6%A0%87%EF%BC%88%E9%9C%80%E8%A6%81%E6%8C%87%E6%A0%87%E6%9C%8D%E5%8A%A1%E5%99%A8%EF%BC%89"><span class="nav-number">26.</span> <span class="nav-text">资源指标（需要指标服务器）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#kuelet%E8%AF%8A%E6%96%AD"><span class="nav-number">27.</span> <span class="nav-text">kuelet诊断</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8Telepresence-%E8%BF%9B%E8%A1%8C%E9%AB%98%E7%BA%A7%E8%B0%83%E8%AF%95"><span class="nav-number">28.</span> <span class="nav-text">使用Telepresence 进行高级调试</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Kubeconfig-%E5%92%8C%E4%B8%8A%E4%B8%8B%E6%96%87"><span class="nav-number">29.</span> <span class="nav-text">Kubeconfig 和上下文</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Pod-%E5%AE%89%E5%85%A8%E6%A0%87%E5%87%86%EF%BC%88PodSecurity-%E5%87%86%E5%85%A5%E6%8E%A7%E5%88%B6%E5%99%A8%EF%BC%89"><span class="nav-number">30.</span> <span class="nav-text">Pod 安全标准（PodSecurity 准入控制器）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Pod-%E4%B8%AD%E6%96%AD%E9%A2%84%E7%AE%97-PDB-%E8%AF%8A%E6%96%AD"><span class="nav-number">31.</span> <span class="nav-text">Pod 中断预算 (PDB) 诊断</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%B5%84%E6%BA%90%E9%94%81%E8%AF%8A%E6%96%AD%EF%BC%88%E5%A6%82%E6%9E%9C%E4%BD%BF%E7%94%A8%E8%B5%84%E6%BA%90%E9%94%81%EF%BC%89"><span class="nav-number">32.</span> <span class="nav-text">资源锁诊断（如果使用资源锁）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9C%8D%E5%8A%A1%E7%AB%AF%E7%82%B9%E5%92%8C-DNS"><span class="nav-number">33.</span> <span class="nav-text">服务端点和 DNS</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Pod-%E4%BC%98%E5%85%88%E7%BA%A7%E5%92%8C%E6%8A%A2%E5%8D%A0"><span class="nav-number">34.</span> <span class="nav-text">Pod 优先级和抢占</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%98%E5%82%A8%E5%8D%B7%E5%BF%AB%E7%85%A7%E8%AF%8A%E6%96%AD%EF%BC%88%E5%A6%82%E6%9E%9C%E4%BD%BF%E7%94%A8%E5%AD%98%E5%82%A8%E5%8D%B7%E5%BF%AB%E7%85%A7%EF%BC%89"><span class="nav-number">35.</span> <span class="nav-text">存储卷快照诊断（如果使用存储卷快照）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%B5%84%E6%BA%90%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E8%AF%8A%E6%96%AD"><span class="nav-number">36.</span> <span class="nav-text">资源反序列化诊断</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%8A%82%E7%82%B9%E6%B1%A1%E7%82%B9"><span class="nav-number">37.</span> <span class="nav-text">节点污点</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9B%B4%E6%94%B9%E5%92%8C%E9%AA%8C%E8%AF%81-Webhook-%E9%85%8D%E7%BD%AE"><span class="nav-number">38.</span> <span class="nav-text">更改和验证 Webhook 配置</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Pod-%E7%BD%91%E7%BB%9C%E7%AD%96%E7%95%A5%EF%BC%9A"><span class="nav-number">39.</span> <span class="nav-text">Pod 网络策略：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%8A%82%E7%82%B9%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E8%AF%A6%E7%BB%86%E4%BF%A1%E6%81%AF"><span class="nav-number">40.</span> <span class="nav-text">节点操作系统详细信息</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Mr Liu"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Mr Liu</p>
  <div class="site-description" itemprop="description">和意识先进的人在一起你才能转变意识，和心态积极的人在一起，你才能够积极，靠自己改变很难，一个人拽着自己的头发是很难把自己拎起来的。</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">24</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="cc-license animated" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="https://cdnjs.cloudflare.com/ajax/libs/creativecommons-vocabulary/2020.11.3/assets/license_badges/big/by_nc_sa.svg" alt="Creative Commons"></a>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://it-liupp.gitee.io/2024/01/19/kubernetes-commands/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Mr Liu">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hello Mr Liu">
      <meta itemprop="description" content="和意识先进的人在一起你才能转变意识，和心态积极的人在一起，你才能够积极，靠自己改变很难，一个人拽着自己的头发是很难把自己拎起来的。">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Kubernetes常用命令 | Hello Mr Liu">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Kubernetes常用命令
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2024-01-19 10:10:10" itemprop="dateCreated datePublished" datetime="2024-01-19T10:10:10+08:00">2024-01-19</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2024-01-23 11:36:31" itemprop="dateModified" datetime="2024-01-23T11:36:31+08:00">2024-01-23</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E8%BD%AF%E4%BB%B6%E4%BD%BF%E7%94%A8/" itemprop="url" rel="index"><span itemprop="name">软件使用</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>2k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>7 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <blockquote>
<p>摘要：记录Kubernetes的常用命令，搜刮自互联网….</p>
</blockquote>
<span id="more"></span>

<p>收藏命令包括但不限于：</p>
<blockquote>
<ul>
<li><p>集群信息</p>
</li>
<li><p>Pod 诊断</p>
</li>
<li><p>服务诊断</p>
</li>
<li><p>部署诊断</p>
</li>
<li><p>网络诊断</p>
<p>……</p>
</li>
</ul>
</blockquote>
<h2 id="集群信息"><a href="#集群信息" class="headerlink" title="集群信息"></a>集群信息</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 显示 Kubernetes 版本：</span>
kubectl version

<span class="token comment"># 显示集群信息：</span>
kubectl cluster-info

<span class="token comment"># 列出集群中的所有节点：</span>
kubectl get nodes

<span class="token comment"># 查看一个具体的节点详情：</span>
kubectl describe <span class="token function">node</span> <span class="token operator">&lt;</span>node-name<span class="token operator">></span>

<span class="token comment"># 列出所有命名空间：</span>
kubectl get namespaces

<span class="token comment"># 列出所有命名空间中的所有 pod：</span>
kubectl get pods --all-namespaces<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="Pod-诊断-amp-amp-健康检查"><a href="#Pod-诊断-amp-amp-健康检查" class="headerlink" title="Pod 诊断&amp;&amp;健康检查"></a>Pod 诊断&amp;&amp;健康检查</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出特定命名空间中的 pod：</span>
kubectl get pods -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 查看一个 Pod 详情：</span>
kubectl describe pod <span class="token operator">&lt;</span>pod-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 查看 Pod 日志：</span>
kubectl logs <span class="token operator">&lt;</span>pod-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 尾部 Pod 日志：</span>
kubectl logs -f <span class="token operator">&lt;</span>pod-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 在 pod 中执行命令：</span>
kubectl <span class="token builtin class-name">exec</span> -it <span class="token operator">&lt;</span>pod-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span> -- <span class="token operator">&lt;</span>command<span class="token operator">></span>

<span class="token comment"># 检查 Pod 准备情况：</span>
kubectl get pods <span class="token operator">&lt;</span>pod-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span> -o <span class="token assign-left variable">jsonpath</span><span class="token operator">=</span><span class="token string">'&#123;.status.conditions[?(@.type=="Ready")].status&#125;'</span>

<span class="token comment"># 检查 Pod 事件：</span>
kubectl get events -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span> --field-selector involvedObject.name<span class="token operator">=</span><span class="token operator">&lt;</span>pod-name<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="Service诊断"><a href="#Service诊断" class="headerlink" title="Service诊断"></a>Service诊断</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出命名空间中的所有服务：kubectl get svc -n &lt;namespace></span>

<span class="token comment"># 查看一个服务详情：kubectl describe svc &lt;service-name> -n &lt;namespace></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<h2 id="Deployment诊断"><a href="#Deployment诊断" class="headerlink" title="Deployment诊断"></a>Deployment诊断</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出命名空间中的所有Deployment：</span>
kubectl get deployments -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 查看一个Deployment详情：</span>
kubectl describe deployment <span class="token operator">&lt;</span>deployment-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 查看滚动发布状态：</span>
kubectl rollout status deployment/<span class="token operator">&lt;</span>deployment-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 查看滚动发布历史记录：</span>
kubectl rollout <span class="token function">history</span> deployment/<span class="token operator">&lt;</span>deployment-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="StatefulSet诊断"><a href="#StatefulSet诊断" class="headerlink" title="StatefulSet诊断"></a>StatefulSet诊断</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出命名空间中的所有 StatefulSet：</span>
kubectl get statefulsets -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 查看一个 StatefulSet详情：</span>
kubectl describe statefulset <span class="token operator">&lt;</span>statefulset-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="ConfigMap-和Secret诊断"><a href="#ConfigMap-和Secret诊断" class="headerlink" title="ConfigMap 和Secret诊断"></a>ConfigMap 和Secret诊断</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出命名空间中的 ConfigMap：</span>
kubectl get configmaps -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 查看一个ConfigMap详情：</span>
kubectl describe configmap <span class="token operator">&lt;</span>configmap-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 列出命名空间中的 Secret：</span>
kubectl get secrets -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 查看一个Secret详情：</span>
kubectl describe secret <span class="token operator">&lt;</span>secret-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="命名空间诊断"><a href="#命名空间诊断" class="headerlink" title="命名空间诊断"></a>命名空间诊断</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 查看一个命名空间详情：</span>
kubectl describe namespace <span class="token operator">&lt;</span>namespace-name<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<h2 id="资源使用情况"><a href="#资源使用情况" class="headerlink" title="资源使用情况"></a>资源使用情况</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 检查 pod 的资源使用情况：</span>
kubectl <span class="token function">top</span> pod <span class="token operator">&lt;</span>pod-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 检查节点资源使用情况：</span>
kubectl <span class="token function">top</span> nodes<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="网络诊断"><a href="#网络诊断" class="headerlink" title="网络诊断"></a>网络诊断</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 显示命名空间中 Pod 的 IP 地址：</span>
kubectl get pods -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span> -o custom-columns<span class="token operator">=</span>POD:metadata.name,IP:status.podIP --no-headers

<span class="token comment"># 列出命名空间中的所有网络策略：</span>
kubectl get networkpolicies -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 查看一个网络策略详情：</span>
kubectl describe networkpolicy <span class="token operator">&lt;</span>network-policy-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="持久卷-PV-和持久卷声明-PVC-诊断"><a href="#持久卷-PV-和持久卷声明-PVC-诊断" class="headerlink" title="持久卷 (PV) 和持久卷声明 (PVC) 诊断"></a>持久卷 (PV) 和持久卷声明 (PVC) 诊断</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出PV：</span>
kubectl get <span class="token function">pv</span>

<span class="token comment"># 查看一个PV详情：</span>
kubectl describe <span class="token function">pv</span> <span class="token operator">&lt;</span>pv-name<span class="token operator">></span>

<span class="token comment"># 列出命名空间中的 PVC：</span>
kubectl get pvc -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 查看PVC详情：</span>
kubectl describe pvc <span class="token operator">&lt;</span>pvc-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="节点诊断"><a href="#节点诊断" class="headerlink" title="节点诊断"></a>节点诊断</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 获取特定节点上运行的 Pod 列表：</span>
kubectl get pods --field-selector spec.nodeName<span class="token operator">=</span><span class="token operator">&lt;</span>node-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<h2 id="资源配额和限制"><a href="#资源配额和限制" class="headerlink" title="资源配额和限制"></a>资源配额和限制</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出命名空间中的资源配额：</span>
kubectl get resourcequotas -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 查看一个资源配额详情：</span>
kubectl describe resourcequota <span class="token operator">&lt;</span>resource-quota-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="自定义资源定义-CRD-诊断"><a href="#自定义资源定义-CRD-诊断" class="headerlink" title="自定义资源定义 (CRD) 诊断"></a>自定义资源定义 (CRD) 诊断</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出命名空间中的自定义资源：</span>
kubectl get <span class="token operator">&lt;</span>custom-resource-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 查看自定义资源详情：</span>
kubectl describe <span class="token operator">&lt;</span>custom-resource-name<span class="token operator">></span> <span class="token operator">&lt;</span>custom-resource-instance-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<blockquote>
<p>使用这些命令时，请记住将<namespace>, <pod-name>, <service-name>, <deployment-name>, <statefulset-name>, <configmap-name>, <secret-name>, <namespace-name>, <pv-name>, <pvc-name>, <node-name>, <network-policy-name>, <resource-quota-name>, <custom-resource-name>, 和替换为你的特定值。</p>
<p><custom-resource-instance-name>这些命令应该可以帮助你诊断 Kubernetes 集群以及在其中运行的应用程序。</p>
</blockquote>
<h2 id="资源伸缩和自动伸缩"><a href="#资源伸缩和自动伸缩" class="headerlink" title="资源伸缩和自动伸缩"></a>资源伸缩和自动伸缩</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># Deployment伸缩：</span>
kubectl scale deployment <span class="token operator">&lt;</span>deployment-name<span class="token operator">></span> --replicas<span class="token operator">=</span><span class="token operator">&lt;</span>replica-count<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 设置Deployment的自动伸缩：</span>
kubectl autoscale deployment <span class="token operator">&lt;</span>deployment-name<span class="token operator">></span> --min<span class="token operator">=</span><span class="token operator">&lt;</span>min-pods<span class="token operator">></span> --max<span class="token operator">=</span><span class="token operator">&lt;</span>max-pods<span class="token operator">></span> --cpu-percent<span class="token operator">=</span><span class="token operator">&lt;</span>cpu-percent<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 检查水平伸缩器状态：</span>
kubectl get hpa -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="作业和-CronJob-诊断"><a href="#作业和-CronJob-诊断" class="headerlink" title="作业和 CronJob 诊断"></a>作业和 CronJob 诊断</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出命名空间中的所有作业：</span>
kubectl get <span class="token function">jobs</span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 查看一份工作详情：</span>
kubectl describe job <span class="token operator">&lt;</span>job-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 列出命名空间中的所有 cron 作业：</span>
kubectl get cronjobs -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 查看一个 cron 作业详情：</span>
kubectl describe cronjob <span class="token operator">&lt;</span>cronjob-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="容量诊断"><a href="#容量诊断" class="headerlink" title="容量诊断"></a>容量诊断</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出按容量排序的持久卷 (PV)：</span>
kubectl get <span class="token function">pv</span> --sort-by<span class="token operator">=</span>.spec.capacity.storage

<span class="token comment"># 查看PV回收策略：</span>
kubectl get <span class="token function">pv</span> <span class="token operator">&lt;</span>pv-name<span class="token operator">></span> -o<span class="token operator">=</span>jsonpath<span class="token operator">=</span><span class="token string">'&#123;.spec.persistentVolumeReclaimPolicy&#125;'</span>

<span class="token comment"># 列出所有存储类别：</span>
kubectl get storageclasses<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="Ingress和服务网格诊断"><a href="#Ingress和服务网格诊断" class="headerlink" title="Ingress和服务网格诊断"></a>Ingress和服务网格诊断</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出命名空间中的所有Ingress：</span>
kubectl get ingress -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 查看一个Ingress详情：</span>
kubectl describe ingress <span class="token operator">&lt;</span>ingress-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 列出命名空间中的所有 </span>
VirtualServices <span class="token punctuation">(</span>Istio<span class="token punctuation">)</span>：kubectl get virtualservices -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 查看一个 VirtualService (Istio)详情：</span>
kubectl describe virtualservice <span class="token operator">&lt;</span>virtualservice-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="Pod-相关"><a href="#Pod-相关" class="headerlink" title="Pod 相关"></a>Pod 相关</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 运行网络诊断 Pod（例如 busybox）进行调试：</span>
kubectl run -it --rm --restart<span class="token operator">=</span>Never --image<span class="token operator">=</span>busybox net-debug-pod -- /bin/sh

<span class="token comment"># 测试从 Pod 到特定端点的连接：</span>
kubectl <span class="token builtin class-name">exec</span> -it <span class="token operator">&lt;</span>pod-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span> -- <span class="token function">curl</span> <span class="token operator">&lt;</span>endpoint-url<span class="token operator">></span>

<span class="token comment"># 跟踪从一个 Pod 到另一个 Pod 的网络路径：</span>
kubectl <span class="token builtin class-name">exec</span> -it <span class="token operator">&lt;</span>source-pod-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span> -- <span class="token function">traceroute</span> <span class="token operator">&lt;</span>destination-pod-ip<span class="token operator">></span>

<span class="token comment"># 检查 Pod 的 DNS 解析：</span>
kubectl <span class="token builtin class-name">exec</span> -it <span class="token operator">&lt;</span>pod-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span> -- <span class="token function">nslookup</span> <span class="token operator">&lt;</span>domain-name<span class="token operator">></span>

<span class="token comment"># 列出 pod 的 pod 亲和性规则：</span>
kubectl get pod <span class="token operator">&lt;</span>pod-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span> -o<span class="token operator">=</span>jsonpath<span class="token operator">=</span><span class="token string">'&#123;.spec.affinity&#125;'</span>

<span class="token comment"># 列出 pod 的 pod 反亲和性规则：</span>
kubectl get pod <span class="token operator">&lt;</span>pod-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span> -o<span class="token operator">=</span>jsonpath<span class="token operator">=</span><span class="token string">'&#123;.spec.affinity.podAntiAffinity&#125;'</span>

<span class="token comment"># 列出所有 Pod 安全策略（如果启用）：</span>
kubectl get psp

<span class="token comment"># 强制删除 pod（不推荐）：</span>
kubectl delete pod <span class="token operator">&lt;</span>pod-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span> --grace-period<span class="token operator">=</span><span class="token number">0</span> --force

<span class="token comment"># 列出 pod 中的开销（k8s 1.18+)：</span>
kubectl get pod <span class="token operator">&lt;</span>pod-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span> -o<span class="token operator">=</span>jsonpath<span class="token operator">=</span><span class="token string">'&#123;.spec.overhead&#125;'</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="配置和资源验证"><a href="#配置和资源验证" class="headerlink" title="配置和资源验证"></a>配置和资源验证</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 验证 Kubernetes YAML 文件而不应用它：</span>
kubectl apply --dry-run<span class="token operator">=</span>client -f <span class="token operator">&lt;</span>yaml-file<span class="token operator">></span>

<span class="token comment"># 验证 pod 的安全上下文和功能：</span>
kubectl auth can-i list pods --as<span class="token operator">=</span>system:serviceaccount:<span class="token operator">&lt;</span>namespace<span class="token operator">></span>:<span class="token operator">&lt;</span>serviceaccount-name<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="RBAC-和安全性"><a href="#RBAC-和安全性" class="headerlink" title="RBAC 和安全性"></a>RBAC 和安全性</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出命名空间中的角色和角色绑定：</span>
kubectl get roles,rolebindings -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 查看角色或角色绑定详情：</span>
kubectl describe role <span class="token operator">&lt;</span>role-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="服务帐户诊断"><a href="#服务帐户诊断" class="headerlink" title="服务帐户诊断"></a>服务帐户诊断</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出命名空间中的服务帐户：</span>
kubectl get serviceaccounts -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 查看一个服务帐户详情：</span>
kubectl describe serviceaccount <span class="token operator">&lt;</span>serviceaccount-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="清空节点和解除封锁"><a href="#清空节点和解除封锁" class="headerlink" title="清空节点和解除封锁"></a>清空节点和解除封锁</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 清空节点以进行维护：kubectl drain &lt;node-name> --ignore-daemonsets</span>

<span class="token comment"># 解除对节点的封锁：kubectl uncordon &lt;node-name></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<h2 id="事件"><a href="#事件" class="headerlink" title="事件"></a>事件</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 查看最近的集群事件：</span>
kubectl get events --sort-by<span class="token operator">=</span>.metadata.creationTimestamp

<span class="token comment"># 按特定命名空间过滤事件：</span>
kubectl get events -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="节点故障排除"><a href="#节点故障排除" class="headerlink" title="节点故障排除"></a>节点故障排除</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 检查节点情况：</span>
kubectl describe <span class="token function">node</span> <span class="token operator">&lt;</span>node-name<span class="token operator">></span> <span class="token operator">|</span> <span class="token function">grep</span> Conditions -A5

<span class="token comment"># 列出节点容量和可分配资源：</span>
kubectl describe <span class="token function">node</span> <span class="token operator">&lt;</span>node-name<span class="token operator">></span> <span class="token operator">|</span> <span class="token function">grep</span> -E <span class="token string">"Capacity|Allocatable"</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="临时容器（Kubernetes-1-18-）"><a href="#临时容器（Kubernetes-1-18-）" class="headerlink" title="临时容器（Kubernetes 1.18+）"></a>临时容器（Kubernetes 1.18+）</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 运行临时调试容器：</span>
kubectl debug -it <span class="token operator">&lt;</span>pod-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span> --image<span class="token operator">=</span><span class="token operator">&lt;</span>debug-image<span class="token operator">></span> -- /bin/sh<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<h2 id="资源指标（需要指标服务器）"><a href="#资源指标（需要指标服务器）" class="headerlink" title="资源指标（需要指标服务器）"></a>资源指标（需要指标服务器）</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 获取 Pod 的 CPU 和内存使用情况：</span>
kubectl <span class="token function">top</span> pod -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<h2 id="kuelet诊断"><a href="#kuelet诊断" class="headerlink" title="kuelet诊断"></a>kuelet诊断</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 查看节点上的kubelet日志：</span>
kubectl logs -n kube-system kubelet-<span class="token operator">&lt;</span>node-name<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<h2 id="使用Telepresence-进行高级调试"><a href="#使用Telepresence-进行高级调试" class="headerlink" title="使用Telepresence 进行高级调试"></a>使用Telepresence 进行高级调试</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 使用 Telepresence 调试 pod：</span>
telepresence --namespace <span class="token operator">&lt;</span>namespace<span class="token operator">></span> --swap-deployment <span class="token operator">&lt;</span>pod-name<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<h2 id="Kubeconfig-和上下文"><a href="#Kubeconfig-和上下文" class="headerlink" title="Kubeconfig 和上下文"></a>Kubeconfig 和上下文</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出可用的上下文：</span>
kubectl config get-contexts

<span class="token comment"># 切换到不同的上下文：</span>
kubectl config use-context <span class="token operator">&lt;</span>context-name<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="Pod-安全标准（PodSecurity-准入控制器）"><a href="#Pod-安全标准（PodSecurity-准入控制器）" class="headerlink" title="Pod 安全标准（PodSecurity 准入控制器）"></a>Pod 安全标准（PodSecurity 准入控制器）</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出 PodSecurityPolicy (PSP) 违规行为：</span>
kubectl get psp -A <span class="token operator">|</span> <span class="token function">grep</span> -vE <span class="token string">'NAME|REVIEWED'</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<h2 id="Pod-中断预算-PDB-诊断"><a href="#Pod-中断预算-PDB-诊断" class="headerlink" title="Pod 中断预算 (PDB) 诊断"></a>Pod 中断预算 (PDB) 诊断</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出命名空间中的所有 PDB：</span>
kubectl get pdb -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 查看一个PDB详情：</span>
kubectl describe pdb <span class="token operator">&lt;</span>pdb-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="资源锁诊断（如果使用资源锁）"><a href="#资源锁诊断（如果使用资源锁）" class="headerlink" title="资源锁诊断（如果使用资源锁）"></a>资源锁诊断（如果使用资源锁）</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出命名空间中的资源锁：</span>
kubectl get resourcelocks -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<h2 id="服务端点和-DNS"><a href="#服务端点和-DNS" class="headerlink" title="服务端点和 DNS"></a>服务端点和 DNS</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出服务的服务端点：</span>
kubectl get endpoints <span class="token operator">&lt;</span>service-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 检查 Pod 中的 DNS 配置：</span>
kubectl <span class="token builtin class-name">exec</span> -it <span class="token operator">&lt;</span>pod-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span> -- <span class="token function">cat</span> /etc/resolv.conf<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="Pod-优先级和抢占"><a href="#Pod-优先级和抢占" class="headerlink" title="Pod 优先级和抢占"></a>Pod 优先级和抢占</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出优先级：</span>
kubectl get priorityclasses<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<h2 id="存储卷快照诊断（如果使用存储卷快照）"><a href="#存储卷快照诊断（如果使用存储卷快照）" class="headerlink" title="存储卷快照诊断（如果使用存储卷快照）"></a>存储卷快照诊断（如果使用存储卷快照）</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出存储卷快照：</span>
kubectl get volumesnapshot -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span>

<span class="token comment"># 查看存储卷快照详情：</span>
kubectl describe volumesnapshot <span class="token operator">&lt;</span>snapshot-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="资源反序列化诊断"><a href="#资源反序列化诊断" class="headerlink" title="资源反序列化诊断"></a>资源反序列化诊断</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 反序列化并打印 Kubernetes 资源：</span>
kubectl get <span class="token operator">&lt;</span>resource-type<span class="token operator">></span> <span class="token operator">&lt;</span>resource-name<span class="token operator">></span> -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span> -o<span class="token operator">=</span>json<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<h2 id="节点污点"><a href="#节点污点" class="headerlink" title="节点污点"></a>节点污点</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出节点污点：</span>
kubectl describe <span class="token function">node</span> <span class="token operator">&lt;</span>node-name<span class="token operator">></span> <span class="token operator">|</span> <span class="token function">grep</span> Taints<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<h2 id="更改和验证-Webhook-配置"><a href="#更改和验证-Webhook-配置" class="headerlink" title="更改和验证 Webhook 配置"></a>更改和验证 Webhook 配置</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出变异 webhook 配置：</span>
kubectl get mutatingwebhookconfigurations

<span class="token comment"># 列出验证 Webhook 配置：</span>
kubectl get validatingwebhookconfigurations<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="Pod-网络策略："><a href="#Pod-网络策略：" class="headerlink" title="Pod 网络策略："></a>Pod 网络策略：</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 列出命名空间中的 pod 网络策略：</span>
kubectl get networkpolicies -n <span class="token operator">&lt;</span>namespace<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<h2 id="节点操作系统详细信息"><a href="#节点操作系统详细信息" class="headerlink" title="节点操作系统详细信息"></a>节点操作系统详细信息</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 获取节点的操作系统信息：</span>
kubectl get <span class="token function">node</span> <span class="token operator">&lt;</span>node-name<span class="token operator">></span> -o <span class="token assign-left variable">jsonpath</span><span class="token operator">=</span><span class="token string">'&#123;.status.nodeInfo.osImage&#125;'</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>



<p>以上这些命令应该涵盖 Kubernetes 中的各种诊断场景。确保将<namespace>、<pod-name>、<deployment-name>等占位符替换为你的集群和用例的实际值。</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="reward-container">
  <div>☕请作者喝杯咖啡☕</div>
  <button>
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wechatpay.jpg" alt="Mr Liu 微信">
        <span>微信</span>
      </div>
      <div>
        <img src="/images/alipay.jpg" alt="Mr Liu 支付宝">
        <span>支付宝</span>
      </div>

  </div>
</div>

          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Mr Liu
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://it-liupp.gitee.io/2024/01/19/kubernetes-commands/" title="Kubernetes常用命令">https://it-liupp.gitee.io/2024/01/19/kubernetes-commands/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/tags/Kubernetes/" rel="tag"># Kubernetes</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2024/01/19/openeuler-install-heketi-k8s/" rel="prev" title="OpenEuler22.03的Heketi安装及K8s、Kubesphere的对接">
                  <i class="fa fa-chevron-left"></i> OpenEuler22.03的Heketi安装及K8s、Kubesphere的对接
                </a>
            </div>
            <div class="post-nav-item">
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Mr Liu</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">42k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">2:32</span>
  </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script>

  




  





</body>
</html>
